package com.xiaodu.acer.phoneplay;

import android.app.Application;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.view.View;

import com.mikepenz.iconics.Iconics;
import com.orhanobut.logger.AndroidLogAdapter;
import com.orhanobut.logger.Logger;
import com.xiaodu.acer.phoneplay.bean.DaoMaster;
import com.xiaodu.acer.phoneplay.bean.DaoSession;
import com.xiaodu.acer.phoneplay.common.font.CustomFont;
import com.xiaodu.acer.phoneplay.di.component.AppComponent;
import com.xiaodu.acer.phoneplay.di.component.DaggerAppComponent;
import com.xiaodu.acer.phoneplay.di.module.AppModule;
import com.xiaodu.acer.phoneplay.di.module.HttpModule;


/**
 * @author 小杜 QQ:1392229149
 * @name phone
 * @class name：com.xiaodu.acer.phoneplay
 * @class describe
 * @time 2017/12/17 11:30
 * @change
 * @chang time
 * @class describe
 */
public class App extends Application {

    private static DaoSession daoSession;
    private View mView;

    private AppComponent mAppComponent;

    public View getView() {
        return mView;
    }

    public void setView(View view) {
        mView = view;
    }

    public static App get(Context context) {
        return (App) context.getApplicationContext();
    }

    public AppComponent getAppComponent() {
        return mAppComponent;
    }


    @Override
    public void onCreate() {
        super.onCreate();

        //only required if you add a custom or generic font on your own
        Iconics.init(getApplicationContext());
        //register custom fonts like this (or also provide a font definition file)
        Iconics.registerFont(new CustomFont());


        Logger.addLogAdapter(new AndroidLogAdapter() {
            @Override
            public boolean isLoggable(int priority, String tag) {
                return BuildConfig.DEBUG;
            }
        });

        mAppComponent = DaggerAppComponent.builder().appModule(new AppModule(this))
                .httpModule(new HttpModule()).build();


        //配置数据库
        setupDatabase();
    }


    /*
      * 配置数据库
      */
    private void setupDatabase() {
        //创建数据库shop.db"
        DaoMaster.DevOpenHelper helper = new DaoMaster.DevOpenHelper(this, "phone.db", null);
        //获取可写数据库
        SQLiteDatabase db = helper.getWritableDatabase();
        //获取数据库对象
        DaoMaster daoMaster = new DaoMaster(db);
        //获取Dao对象管理者
        daoSession = daoMaster.newSession();
    }

    public static DaoSession getDaoInstant() {
        return daoSession;
    }
}
